#include <linux/module.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/fs.h>

MODULE_LICENSE ("GPL");

//设备号
dev_t dev; 
unsigned int major = 0;
unsigned int minor = 10;

//定义一个cdev对象
struct cdev led_cdev; 

int led_open  (struct inode *inode, struct file * filp)
{
	printk (KERN_ERR "enter %s\n", __func__);
	return 0; 
}
int led_release (struct inode *inode, struct file* filp)
{
	printk (KERN_ERR "enter %s\n", __func__);
	return 0;
}

struct file_operations led_fops = 
{
	.owner = THIS_MODULE,
	.open = led_open,
	.release = led_release,
};

int __init led_drv_init (void)
{
	//dev = (major << 20) | minor;//自己实现
	//静态注册
	if (major) {
		dev = MKDEV (major, minor);
		register_chrdev_region (dev, 1, "leds");
	} else {
		//动态注册
		alloc_chrdev_region (&dev, minor, 1, "leds");
		major = MAJOR (dev);
		minor = MINOR (dev);
		printk (KERN_ERR "major = %d, minor = %d\n", major, minor);
	}

	//1) 注册cdev 
	cdev_init (&led_cdev, &led_fops);
	//2) 增加cdev对象到内核
	cdev_add (&led_cdev, dev, 1);
	return 0;
}
void __exit led_drv_exit (void)
{
	//3) 注销cdev
	cdev_del (&led_cdev);

	// 注销设备号
	unregister_chrdev_region (dev, 1);
	return ;
}
module_init (led_drv_init);
module_exit (led_drv_exit);
